home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Risc World 3
/
Risc World 3.iso
/
SOFTWARE
/
ISSUE6
/
PD
/
PDF
/
pdf
/
c++
/
ColourSpace
< prev
next >
Wrap
Text File
|
2003-02-14
|
6KB
|
164 lines
//--------------------------------------------------------------------------
//
// Copyright (c) 2002, Colin Granville
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or
// without modification, are permitted provided that the following
// conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// * The name Colin Granville may not be used to endorse or promote
// products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
//
//--------------------------------------------------------------------------
#include "ColourSpace.h"
#include "GfxState.h"
inline unsigned int rgb(unsigned int r,unsigned int g,unsigned int b)
{
return (r<<8) | (g<<16) | (b<<24);
}
unsigned int DeviceGreyColourSpace::getRGB(const unsigned char* data)
{
return rgb(data[0],data[0],data[0]);
}
unsigned int CalGreyColourSpace::getRGB(const unsigned char* data)
{
return rgb(data[0],data[0],data[0]);
}
unsigned int DeviceRGBColourSpace::getRGB(const unsigned char* data)
{
return rgb(data[0],data[1],data[2]);
}
unsigned int CalRGBColourSpace::getRGB(const unsigned char* data)
{
return rgb(data[0],data[1],data[2]);
}
unsigned int DeviceCMYKColourSpace::getRGB(const unsigned char* data)
{
unsigned int r=255-data[0]-data[3]; if (r>255) r=0;
unsigned int g=255-data[1]-data[3]; if (g>255) g=0;
unsigned int b=255-data[2]-data[3]; if (b>255) b=0;
return rgb(r,g,b);
}
unsigned int LabColourSpace::getRGB(const unsigned char* data)
{
//return RGB as proper conversion will be far too slow
return rgb(data[0],data[1],data[2]);
}
unsigned int ICCBasedColourSpace::getRGB(const unsigned char* data)
{
//return RGB as proper conversion will be far too slow
return rgb(data[0],data[1],data[2]);
}
//***************************************************************************************
IndexedColourSpace::IndexedColourSpace(unsigned char* lookuptab,int ncomps,ColourSpace* b)
: lookup(lookuptab),
numberOfComponents(ncomps),
base(b)
{}
IndexedColourSpace::~IndexedColourSpace() {delete base;}
#include "iostream.h"
unsigned int IndexedColourSpace::getRGB(const unsigned char* data)
{
return base->getRGB(&lookup[data[0]*numberOfComponents]);
}
//***************************************************************************************
unsigned int SeparationColourSpace::getRGB(const unsigned char* data)
{
//return reversed greyscale
unsigned int n=*data^0xff;
return rgb(n,n,n);
}
unsigned int DeviceNColourSpace::getRGB(const unsigned char* data)
{
//return RGB as proper conversion will be far too slow
return rgb(data[0],data[1],data[2]);
}
unsigned int PatternColourSpace::getRGB(const unsigned char*)
{
//return RGB as proper conversion will be far too slow
return 0xffffff00;
}
//*****************************************************************
ColourSpace* makeColourSpace(GfxColorSpace* cs)
{
if (!cs) return 0;
switch (cs->getMode())
{
case csDeviceGray: return new DeviceGreyColourSpace;
case csCalGray: return new CalGreyColourSpace;
case csDeviceRGB: return new DeviceRGBColourSpace;
case csCalRGB: return new CalRGBColourSpace;
case csDeviceCMYK: return new DeviceCMYKColourSpace;
case csLab: return new LabColourSpace;
case csICCBased: {
GfxICCBasedColorSpace* ibcs=(GfxICCBasedColorSpace*)cs;
if (!ibcs->getAlt()) return new ICCBasedColourSpace;
return makeColourSpace(ibcs->getAlt());
}
case csIndexed: {
GfxIndexedColorSpace* ics=(GfxIndexedColorSpace*)cs;
ColourSpace* s=makeColourSpace(ics->getBase());
int ncomps=ics->getBase()->getNComps();
if (!s) {s= new DeviceRGBColourSpace;ncomps=3;}
return new IndexedColourSpace((unsigned char*)ics->getLookup(),ncomps,s);
}
case csSeparation: {
GfxSeparationColorSpace* scs=(GfxSeparationColorSpace*)cs;
return new SeparationColourSpace;
// if (!scs->getAlt()) return new SeparationColourSpace;
// return makeColourSpace(scs->getAlt());
}
case csDeviceN: {
GfxDeviceNColorSpace* dncs=(GfxDeviceNColorSpace*)cs;
if (!dncs->getAlt()) return new DeviceNColourSpace;
return makeColourSpace(dncs->getAlt());
}
case csPattern: return new PatternColourSpace;
}
return 0;
}